"""
Main FastAPI application entry point
"""

from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse, FileResponse
import uvicorn
import asyncio
import logging
import os

import supertokens_python
from supertokens_python.framework.fastapi import get_middleware
from supertokens_python.recipe import session
from supertokens_python.recipe.session.framework.fastapi import verify_session
from supertokens_python.recipe.session.interfaces import SessionContainer
from supertokens_python.recipe.session import SessionRecipe
from supertokens_python.recipe.session.syncio import create_new_session
from supertokens_python import InputAppInfo, SupertokensConfig

from .api.openai_compat import router as openai_router
from .api.landppt_api import router as landppt_router
from .api.database_api import router as database_router
from .api.global_master_template_api import router as template_api_router
from .api.config_api import router as config_router
from .api.image_api import router as image_router

from .web import router as web_router
from .web.embed_routes import router as embed_router
from .auth import auth_router, create_auth_middleware
from .database.database import init_db
from .database.create_default_template import ensure_default_templates_exist_first_time
from .core.config import app_config

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Disable SQLAlchemy verbose logging completely
logging.getLogger('sqlalchemy').setLevel(logging.WARNING)
logging.getLogger('sqlalchemy.engine').setLevel(logging.WARNING)
logging.getLogger('sqlalchemy.engine.Engine').setLevel(logging.WARNING)
logging.getLogger('sqlalchemy.pool').setLevel(logging.WARNING)
logging.getLogger('sqlalchemy.dialects').setLevel(logging.WARNING)

# Get root path from environment or default to /ppt
ROOT_PATH = os.getenv("ROOT_PATH", "/ppt")

# Initialize Supertokens
supertokens_python.init(
    app_info=InputAppInfo(
        app_name="yzs-ppt",
        api_domain=app_config.supertokens_api_domain,
        website_domain=app_config.supertokens_website_domain
    ),
    supertokens_config=SupertokensConfig(
        connection_uri=app_config.supertokens_conn_uri,
        api_key=app_config.supertokens_api_key
    ),
    framework="fastapi",
    recipe_list=[session.init(cookie_domain=app_config.supertokens_cookie_domain)]
)

# Create FastAPI app
app = FastAPI(
    title="LandPPT API",
    description="AI-powered PPT generation platform with OpenAI-compatible API",
    version="0.1.0",
    docs_url="/docs",
    redoc_url="/redoc",
    root_path=ROOT_PATH
)


@app.on_event("startup")
async def startup_event():
    """Initialize database on startup"""
    try:
        # Check if database file exists before initialization
        import os
        db_file_path = "landppt.db"  # 默认数据库文件路径
        db_exists = os.path.exists(db_file_path)

        logger.info("Initializing database...")
        await init_db()
        logger.info("Database initialized successfully")

        # Only import templates if database file didn't exist before (first time setup)
        if not db_exists:
            logger.info("First time setup detected - importing templates from examples...")
            template_ids = await ensure_default_templates_exist_first_time()
            logger.info(f"Template initialization completed. {len(template_ids)} templates available.")
        else:
            logger.info("Database already exists - skipping template import")

    except Exception as e:
        logger.error(f"Failed to initialize application: {e}")
        raise


@app.on_event("shutdown")
async def shutdown_event():
    """Clean up database connections on shutdown"""
    try:
        logger.info("Shutting down application...")
        logger.info("Application shutdown complete")
    except Exception as e:
        logger.error(f"Error during shutdown: {e}")

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=[app_config.supertokens_website_domain],
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
    allow_headers=["Content-Type", "Authorization", "anti-csrf"],
    expose_headers=["Content-Type", "anti-csrf"]
)

# Add Supertokens middleware
app.add_middleware(get_middleware())

# Add root path middleware to handle X-Forwarded-Prefix
@app.middleware("http")
async def root_path_middleware(request: Request, call_next):
    """Handle X-Forwarded-Prefix header for dynamic root path adjustment"""
    forwarded_prefix = request.headers.get("X-Forwarded-Prefix")
    if forwarded_prefix:
        # Update the scope to include the forwarded prefix
        request.scope["root_path"] = forwarded_prefix.rstrip("/")
    response = await call_next(request)
    return response

# Add security headers middleware for embed routes
@app.middleware("http")
async def security_headers_middleware(request: Request, call_next):
    """Add security headers, especially for embed routes"""
    response = await call_next(request)
    
    # Check if this is an embed route
    if request.url.path.startswith("/ppt/embed/"):
        # Allow embedding from yunzhishi.zzz4ai.com
        response.headers["Content-Security-Policy"] = "frame-ancestors https://yunzhishi.zzz4ai.com"
        response.headers["X-Frame-Options"] = "ALLOW-FROM https://yunzhishi.zzz4ai.com"
        
        # Additional security headers for embed
        response.headers["X-Content-Type-Options"] = "nosniff"
        response.headers["X-XSS-Protection"] = "1; mode=block"
        response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
        
    else:
        # For non-embed routes, deny framing
        response.headers["X-Frame-Options"] = "DENY"
        response.headers["Content-Security-Policy"] = "frame-ancestors 'none'"
    
    return response

# Include routers
app.include_router(auth_router, prefix="", tags=["Authentication"])
app.include_router(config_router, prefix="", tags=["Configuration Management"])
app.include_router(image_router, prefix="", tags=["Image Service"])
app.include_router(embed_router, prefix="", tags=["Embed Interface"])

app.include_router(openai_router, prefix="/v1", tags=["OpenAI Compatible"])
app.include_router(landppt_router, prefix="/api", tags=["LandPPT API"])
app.include_router(template_api_router, tags=["Global Master Templates"])
app.include_router(database_router, tags=["Database Management"])
app.include_router(web_router, prefix="", tags=["Web Interface"])

# Mount static files
static_dir = os.path.join(os.path.dirname(__file__), "web", "static")
app.mount("/static", StaticFiles(directory=static_dir), name="static")

# Mount temp directory for image cache
temp_dir = os.path.join(os.getcwd(), "temp")
if os.path.exists(temp_dir):
    app.mount("/temp", StaticFiles(directory=temp_dir), name="temp")
    logger.info(f"Mounted temp directory: {temp_dir}")
else:
    logger.warning(f"Temp directory not found: {temp_dir}")

@app.get("/", response_class=HTMLResponse)
async def root():
    """Root endpoint - redirect to dashboard"""
    from fastapi.responses import RedirectResponse
    return RedirectResponse(url="/dashboard", status_code=302)

@app.get("/favicon.ico")
async def favicon():
    """Serve favicon"""
    favicon_path = os.path.join(os.path.dirname(__file__), "web", "static", "images", "favicon.svg")
    if os.path.exists(favicon_path):
        return FileResponse(favicon_path, media_type="image/svg+xml")
    else:
        raise HTTPException(status_code=404, detail="Favicon not found")

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {"status": "healthy", "service": "LandPPT API"}

if __name__ == "__main__":
    uvicorn.run(
        "src.landppt.main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )
